home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / Xprof / xprof / update.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  204 lines

  1. /*==================================================================
  2.  *      File :          update.c
  3.  *      Package:        Xprof
  4.  *
  5.  *      Author :        Aloke Gupta.
  6.  *
  7.  *  (C) Copyright 1992, Aloke Gupta.
  8.  *==================================================================*/
  9.  
  10. /*
  11.  * update_profile(int index, Xattributes *attributes)
  12.  * print_profile_stats();
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <X11/Xproto.h>
  17. #include "common.h"
  18. #include "profile.h"
  19.  
  20. extern MsgStats RequestStats[];
  21. extern MsgType  RequestType [];
  22.  
  23. /* The following times are maintained in milliseconds */
  24. static ProfileStats profilestats[MAXREQUESTS];
  25.  
  26. /* Functions to update above profilestats */
  27. enter_net_time(index, time)
  28. int index; double time;
  29.     profilestats[index].net_time   += (double) time;
  30.     profilestats[index].total_time += (double) time;
  31. }
  32. enter_cpu_time(index, time)
  33. int index; double time;
  34.     profilestats[index].cpu_time   += (double) time;
  35.     profilestats[index].total_time += (double) time;
  36. }
  37.  
  38. /* init_profile: Initialize the profilestats data structure */
  39. init_profile()
  40. {   int i;
  41.     for (i = 0; i < MAXREQUESTS; i++) {
  42.     profilestats[i].net_time   = 0;
  43.     profilestats[i].cpu_time   = 0;
  44.     profilestats[i].total_time = 0;
  45.     profilestats[i].cpu_valid  = VALID;    /* Mark it valid by default */
  46.     }
  47. }
  48.  
  49. /* 
  50.  * update_profile: Make entries in the data structures for network time and
  51.  * server computation time.
  52.  */
  53. update_profile(index, attributes)
  54. int  index;             /* Index-number of request  */
  55. Xattributes *attributes;    /* Attributes of the message */
  56. {
  57.     double net_time=0.0;
  58.     double cpu_time=0.0;
  59.     /*
  60.      * First update the network time for this function
  61.      */
  62.     if (attributes->bytes != -1) {
  63.         /* Network Speed contribution */
  64.         net_time = (double) 1000.0 * attributes->bytes / get_net_speed();
  65.         enter_net_time(index, net_time);
  66.         /* Network Latency contribution */
  67.         switch (index) {    /* Only counted for round-trip requests */
  68.         case X_GetWindowAttributes:         /*  3 */
  69.         case X_GetGeometry:            /* 14 */
  70.         case X_QueryTree:            /* 15 */
  71.         case X_InternAtom:            /* 16 */
  72.         case X_GetAtomName:            /* 17 */
  73.         case X_GetProperty:            /* 20 */
  74.         case X_ListProperties:            /* 21 */
  75.         case X_GetSelectionOwner:        /* 23 */
  76.         case X_GrabPointer:            /* 26 */
  77.         case X_GrabKeyboard:            /* 31 */
  78.         case X_QueryPointer:            /* 38 */
  79.         case X_GetMotionEvents:            /* 39 */
  80.         case X_TranslateCoords:            /* 40 */
  81.         case X_GetInputFocus:            /* 43 */
  82.         case X_QueryKeymap:            /* 44 */
  83.         case X_QueryFont:            /* 47 */
  84.         case X_QueryTextExtents:        /* 48 */
  85.         case X_ListFonts:            /* 49 */
  86.         case X_ListFontsWithInfo:        /* 50 */
  87.         case X_GetFontPath:            /* 52 */
  88.         case X_GetImage:            /* 73 */
  89.         case X_ListInstalledColormaps:        /* 83 */
  90.         case X_AllocColor:            /* 84 */
  91.         case X_AllocNamedColor:            /* 85 */
  92.         case X_AllocColorCells:            /* 86 */
  93.         case X_AllocColorPlanes:        /* 87 */
  94.         case X_QueryColors:            /* 91 */
  95.         case X_LookupColor:            /* 92 */
  96.         case X_QueryBestSize:            /* 97 */
  97.         case X_QueryExtension:            /* 98 */
  98.         case X_ListExtensions:            /* 99 */
  99.         case X_GetKeyboardMapping:        /*101 */
  100.         case X_GetKeyboardControl:        /*103 */
  101.         case X_GetPointerControl:        /*106 */
  102.         case X_GetScreenSaver:            /*108 */
  103.         case X_ListHosts:            /*110 */
  104.         case X_SetPointerMapping:        /*116 */
  105.         case X_GetPointerMapping:        /*117 */
  106.         case X_SetModifierMapping:        /*118 */
  107.         case X_GetModifierMapping:        /*119 */
  108.             enter_net_time(index, (double) get_net_latency());
  109.             break;
  110.         default:        /* All other requests are one-way */
  111.             break;
  112.         }    /* switch(index) */
  113.     }
  114.  
  115.     /*
  116.      * Now update the cputime for this request
  117.      */
  118.     if ((attributes->size!=-1)&&(profilestats[index].cpu_valid!=INVALID)) {
  119.         cpu_time = (double) get_request_time(index, attributes);
  120.         if (cpu_time != -1.0) {
  121.         cpu_time *= 1000.0;        /* ms */
  122.         enter_cpu_time(index, cpu_time);
  123.         }
  124.         /* Else mark the cpu time invalid */
  125.         else profilestats[index].cpu_valid = INVALID ;
  126.     }
  127. }
  128.  
  129. static char *profile_header[] = {
  130. "_____________________________________________________________________________",
  131. "Request                     Time % of  Compute Network No. of % of  Time/call",
  132. " Name                       (ms) total part(%) part(%)  msgs  total   (ms)",
  133. };
  134. /* The following functions uses the RequestStats[] and RequestType[] structure
  135.  * in addition to the profilestats structure maintained in this file.
  136.  */
  137. print_profile_stats(fp)
  138. FILE *fp;
  139. {
  140.     int    i;
  141.     long   total_num = 0;
  142.     double total_net_time  = 0.0;
  143.     double total_cpu_time  = 0.0;
  144.     double total_total_time= 0.0;
  145.  
  146.     fprintf(fp, "\n \t\t***** Execution Profile *****\n");
  147.     /* Print Network details */
  148.     if (get_net_speed() == MAXDOUBLE)
  149.      fprintf(fp,"\nNetwork speed = Infinity,");
  150.     else fprintf(fp,"\nNetwork speed = %.2lf Kbytes/sec,",
  151.         (double) get_net_speed() / 1000.0);
  152.     fprintf(fp," latency = %.2lf ms\n", (double) get_net_latency());
  153.  
  154.     /* Accumulate totals */
  155.     for (i = 0; i < MAXREQUESTS; i++) 
  156.       if (RequestStats[i].invoked == TRUE)  {
  157.     total_num     += RequestStats[i].number;
  158.     total_net_time   += profilestats[i].net_time  ;
  159.     total_cpu_time   += profilestats[i].cpu_time  ;
  160.     total_total_time += profilestats[i].total_time;
  161.       }
  162.     /*
  163.      * Print the header
  164.      */
  165.      fprintf(fp,"%s\n", profile_header[0]);
  166.      fprintf(fp,"%s\n", profile_header[1]);
  167.      fprintf(fp,"%s\n", profile_header[2]);
  168.      fprintf(fp,"%s\n", profile_header[0]);
  169.     /*
  170.      * Print the statistics
  171.      */
  172.     for (i = 0; i < MAXREQUESTS; i++) 
  173.       if (RequestStats[i].invoked == TRUE) {
  174.     fprintf(fp,"%-22s",     RequestType[i].name);
  175.     fprintf(fp,"%10.3lf " , profilestats[i].total_time);
  176.     fprintf(fp,"%5.2lf%% ",
  177.         (double) 100.0 * profilestats[i].total_time / total_total_time);
  178.         if (profilestats[i].cpu_valid == VALID)
  179.          fprintf(fp,"%5.2lf%% ",
  180.         (double) 100.0 * profilestats[i].cpu_time / total_total_time);
  181.     else fprintf(fp,"  N/C  ");
  182.     fprintf(fp,"%5.2lf%% ",
  183.         (double) 100.0 * profilestats[i].net_time / total_total_time);
  184.     fprintf(fp,"%6ld "    , RequestStats[i].number);
  185.     fprintf(fp,"%5.2lf%% ",
  186.         (double) 100.0 * RequestStats[i].number / total_num);
  187.     fprintf(fp,"%7.3lf\n",
  188.         (double) (profilestats[i].total_time / RequestStats[i].number));
  189.       }
  190.     fprintf(fp,"%s\n", profile_header[0]);
  191.     fprintf(fp,"%22s%10.3lf %5.1lf%% %5.2lf%% %5.2lf%% %6ld %5.1lf%% %7.3lf\n",
  192.     "Grand Total  ",
  193.     total_total_time,
  194.     100.0,
  195.     (double) 100.0 * total_cpu_time / total_total_time,
  196.     (double) 100.0 * total_net_time / total_total_time,
  197.     total_num,
  198.     100.0,
  199.     (double) total_total_time / total_num);
  200.     fprintf(fp,"%s\n", profile_header[0]);
  201. }
  202.